enum Style {
    //% block="不限定风格"
    000,
    //% block="水墨画"
    101,
    //% block="概念艺术"
    102,
    //% block="油画1"
    103,
    //% block="油画2（梵高）"
    118,
    //% block="水彩画"
    104,
    //% block="像素画"
    105,
    //% block="厚涂风格"
    106,
    //% block="插图"
    107,
    //% block="剪纸风格"
    108,
    //% block="印象派1（莫奈）"
    109,
    //% block="印象派2"
    119,
    //% block="日系动漫"
    201,
    //% block="怪兽风格"
    202,
    //% block="唯美古风"
    203,
    //% block="科幻风格"
    114,
    //% block="复古动漫"
    204,
    //% block="游戏卡通手绘"
    301,
    //% block="通用写实风格"
    401,
}

//% color="#5bc2e8" iconWidth=50 iconHeight=40
namespace tenxunAi{
//% block="初始化腾讯ai文生图模块" blockType="command"
export function tenxun__init(parameter: any, block: any) {
Generator.addImport(`import json\nimport types\nimport numpy as np\nfrom tencentcloud.common import credential\nfrom tencentcloud.common.profile.client_profile import ClientProfile\nfrom tencentcloud.common.profile.http_profile import HttpProfile\nfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException\nfrom tencentcloud.hunyuan.v20230901 import hunyuan_client, models\nimport base64\nimport cv2`)
}

//% block="加载tenxunai模型SecretId:[SecretId],SecretKey:[SecretKey]" blockType="command"
//% SecretId.shadow="normal" SecretId.defl="XXXXX"
//% SecretKey.shadow="normal" SecretKey.defl="XXXXX"
export function tenxun_id(parameter: any, block: any) {
let SecretId=parameter.SecretId.code
let SecretKey=parameter.SecretKey.code
Generator.addCode(`
cred = credential.Credential("${SecretId}","${SecretKey}")
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
#实例化一个client选项，可选的，没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", clientProfile)`)
} 

//% block="文本内容:[TXT]，风格[style],反向描述[Ms]，画面大小[size]" blockType="command"
//% TXT.shadow="normal" TXT.defl="林海，飞鸟，河边，青年"
//% style.shadow="dropdown" style.options="Style" style.defl="Style.102"
//% size.shadow="normal" size.defl="1920:1080"
//% Ms.shadow="string" Ms.defl="模糊，扭曲"
export function tenxun_content(parameter: any, block: any) {
let TXT=parameter.TXT.code
let style=parameter.style.code
let size=parameter.size.code
let Ms=parameter.Ms.code
Generator.addCode(`
req = models.TextToImageLiteRequest()
params = {
    "Prompt":${TXT},
    "NegativePrompt":  ${Ms},
    "Style":"${style}",
    "Resolution":"${size}"
}
req.from_json_string(json.dumps(params))
#返回的resp是一个TextToImageResponse的实例，与请求对象对应
resp = client.TextToImageLite(req)`)
} 

//% block="保存图片名称:[Img],宽:[width],高:[Height]" blockType="command"
//% Img.shadow="normal" Img.defl="tengxunai_01.png"
//% width.shadow="number" width.defl="240"
//% Height.shadow="number" Height.defl="320"
export function tenxun_Img(parameter: any, block: any) {
let Img=parameter.Img.code
let width=parameter.width.code
let Height=parameter.Height.code
Generator.addCode(`
image_base64 = resp.ResultImage
image_data = base64.b64decode(image_base64)
np_array=np.frombuffer(image_data,np.uint8)
image=cv2.imdecode(np_array,cv2.IMREAD_COLOR)
image=cv2.resize(image,(${width},${Height}))
cv2.imwrite("${Img}", image)`)} 
}